package com.doordu.soa.service.web.auth.service;

import java.util.Calendar;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.doordu.soa.service.comm.constant.Constant;
import com.doordu.soa.service.comm.redis.RedisDoorduTemplate;
import com.doordu.soa.service.web.auth.TokenManager;
import com.doordu.soa.service.web.auth.model.TokenModel;

/**
 * token redis manager
 * 
 * @ClassName: RedisTokenManager
 * @Description:
 * @author xiaoqun.yi yixq@doordu.com
 * @date 2017年4月6日 下午5:55:21
 * @version V1.0
 */
@Service
public class RedisTokenManager implements TokenManager {
	protected static final Logger log = LoggerFactory.getLogger(RedisTokenManager.class);
	@Autowired
	private RedisDoorduTemplate redis;
	
	@Value("${spring.profiles.active}")
	private String springProfilesActive;
	
	public Integer checkToken(TokenModel model) {
		if (model == null) {
			return 1;
		}
		String tokenKey = tokenKeyPrefix + "_" + model.getAccess_token();
		TokenModel tokenMoel = redis.get(tokenKey);
		if (null == tokenMoel) {
			return 1;
		}
		// 如果验证成功，说明此用户进行了一次有效操作，延长token的过期时间
		// Calendar cal = Calendar.getInstance();
		// cal.add(Calendar.HOUR_OF_DAY, Constant.TOKEN_EXPIRES_HOUR);
		redis.set(tokenKey, tokenMoel);
		redis.expire(tokenKey, Constant.LOGIN_TOKEN_EXPIRES_SECOND);
		return 0;
	}

	public void deleteToken(String appId) {
		String tokenKey = tokenKeyPrefix + appId;
		redis.delete(tokenKey);
	}

	@Override
	public TokenModel createToken(String appId, String appSecret, Integer companyId) {
		// 使用uuid作为源token
		// String token = UUID.randomUUID().toString().replace("-", "");
		String token_data = appSecret + System.nanoTime();
		String token = DigestUtils.md5Hex(token_data);
		TokenModel tokenModel = new TokenModel(appId, token, companyId);
		// 存储到redis并设置过期时间
		String tokenKey = tokenKeyPrefix + "_" + token;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, Constant.TOKEN_EXPIRES_HOUR);
		redis.set(tokenKey, tokenModel);
		redis.expireAt(tokenKey, cal.getTime());
		tokenModel.setExpired_at(String.valueOf(cal.getTimeInMillis()));
		// redis.expire(tokenKey, 60*20);
		return tokenModel;
	}

	@Override
	public TokenModel getTokenModelByToken(String token) {
		String tokenKey = tokenKeyPrefix + "_" + token;
		TokenModel tokenMoel = redis.get(tokenKey);
		return tokenMoel;
	}

	@Override
	public void tokenAddExpired_at(String token) {
		String tokenKey = tokenKeyPrefix + "_" + token;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, Constant.TOKEN_EXPIRES_HOUR);
		redis.expireAt(tokenKey, cal.getTime());
		
	}

	public String getSpringProfilesActive() {
		return springProfilesActive;
	}

	public void setSpringProfilesActive(String springProfilesActive) {
		this.springProfilesActive = springProfilesActive;
	}
}